home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / New System Software Extensions / OpenDoc A6 / OpenDoc Parts Framework / OPF / Examples / Movie / Sources / MovieFac.cpp next >
Encoding:
Text File  |  1994-04-21  |  17.9 KB  |  626 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                CMovieFac.cpp
  4. //    Release Version:    $ 1.0d1 $
  5. //
  6. //    Author:                Lonnie Millett
  7. //    Creation Date:        3/28/94
  8. //
  9. //    Copyright:    © 1993, 1994 by Apple Computer, Inc., all rights reserved.
  10. //
  11. //========================================================================================
  12.  
  13. #ifndef MOVIEFAC_H
  14. #include "MovieFac.h"
  15. #endif
  16.  
  17. #ifndef MOVIEFRA_H
  18. #include "MovieFra.h"
  19. #endif
  20.  
  21. #ifndef MOVIEPAR_H
  22. #include "MoviePar.h"
  23. #endif
  24.  
  25. #ifndef MOVIESEL_H
  26. #include "MovieSel.h"
  27. #endif
  28.  
  29. // ----- Framework Includes -----
  30.  
  31. #ifndef FWMEMMGR_H
  32. #include <FWMemMgr.h>
  33. #endif
  34.  
  35. #ifndef FWGRDEF_H
  36. #include "FWGrDef.h"
  37. #endif
  38.  
  39. #ifndef FWUTIL_H
  40. #include "FWUtil.h"
  41. #endif
  42.  
  43. #ifndef FWRECT_H
  44. #include "FWRect.h"
  45. #endif
  46.  
  47. // ----- OpenDoc Includes -----
  48.  
  49. #ifndef _FACET_
  50. #include <Facet.h>
  51. #endif
  52.  
  53. #ifndef _FRAME_
  54. #include <Frame.h>
  55. #endif
  56.  
  57. #ifndef _SHAPE_
  58. #include <Shape.h>
  59. #endif
  60.  
  61. #ifndef _WINDOW_
  62. #include <Window.h>
  63. #endif
  64.  
  65. #ifndef _TRNSFRMM_
  66. #include <TrnsfrmM.h>
  67. #endif
  68.  
  69. #ifndef _EXCEPT_
  70. #include <Except.h>
  71. #endif
  72.  
  73. #ifndef _XMPSESSM_
  74. #include <XMPSessM.h>
  75. #endif
  76.  
  77. #ifndef _TRANSLAT_
  78. #include <Translat.h>
  79. #endif
  80.  
  81. #ifndef _DRAGDROP_
  82. #include <DragDrop.h>
  83. #endif
  84.  
  85. #ifndef _DGITMITR_
  86. #include <DgItmItr.h>
  87. #endif
  88.  
  89. #ifndef _STDPROPS_
  90. #include <StdProps.h>
  91. #endif
  92.  
  93. #ifndef _STDTYPES_
  94. #include <StdTypes.h>
  95. #endif
  96.  
  97. #ifndef _STORAGEU_
  98. #include <StorageU.h>
  99. #endif
  100.  
  101. #if defined(FW_BUILD_MAC) && !defined(__QUICKDRAW__)
  102. #include <Quickdraw.h>
  103. #endif
  104.  
  105. #if defined(FW_BUILD_MAC) && !defined(__RESOURCES__)
  106. #include <Resources.h>
  107. #endif
  108.  
  109. #if defined(FW_BUILD_MAC) && !defined(mathRoutinesIncludes)
  110. #include <math routines.h>
  111. #endif
  112.  
  113. #pragma segment MoviePart
  114.  
  115. //----------------------------------------------------------------------------------------
  116. // CMovieFacet::CMovieFacet
  117. //----------------------------------------------------------------------------------------
  118. CMovieFacet::CMovieFacet()
  119. {
  120.     fMovie = NULL;
  121.     fMovieController = NULL;
  122. }
  123.  
  124. //----------------------------------------------------------------------------------------
  125. // CMovieFacet::~CMovieFacet
  126. //----------------------------------------------------------------------------------------
  127. CMovieFacet::~CMovieFacet()
  128. {
  129.     if (fMovieController)
  130.     {
  131.         DisposeMovieController(fMovieController);
  132.         fMovieController = NULL;
  133.     }
  134.     
  135.     if (fMovie)
  136.     {
  137.         DisposeMovie(fMovie);
  138.         fMovie = NULL;
  139.     }
  140. }
  141.  
  142. //----------------------------------------------------------------------------------------
  143. // CMovieFacet::InitMovieFacet
  144. //----------------------------------------------------------------------------------------
  145. void CMovieFacet::InitMovieFacet(XMPFacet* xmpFacet, CMovieFrame* movieFrame)
  146. {
  147.     this->InitFacet(xmpFacet);
  148.     fMovieFrame = movieFrame;
  149. }
  150.  
  151. //----------------------------------------------------------------------------------------
  152. // CMovieFacet::GetMoviePart()
  153. //----------------------------------------------------------------------------------------
  154. inline CMoviePart* CMovieFacet::GetMoviePart() const
  155. {
  156.     return fMovieFrame->GetMoviePart();
  157. }
  158.  
  159. //----------------------------------------------------------------------------------------
  160. // CMovieFacet::GetMovieBoundingBox
  161. //----------------------------------------------------------------------------------------
  162. void CMovieFacet::GetMovieBoundingBox(FW_CRect* boundingBox)
  163. {
  164.     if (fMovie)
  165.     {
  166.         FW_SPlatformRect movieBox;
  167.         ::GetMovieBox(fMovie, &movieBox);
  168.         
  169.         if (this->GetMovieFrame()->IsActive())
  170.             movieBox.bottom += 16;
  171.         *boundingBox = movieBox;
  172.     }
  173.     else
  174.         *boundingBox = this->GetMoviePart()->GetDefaultPicture()->GetRectangle();
  175. }
  176.  
  177. //----------------------------------------------------------------------------------------
  178. // CMovieFacet::SetMovie
  179. //----------------------------------------------------------------------------------------
  180. void CMovieFacet::SetMovie(Movie newMovie)
  181. {
  182.     if (newMovie)
  183.     {
  184.         if (fMovieController)
  185.         {
  186.             DisposeMovieController(fMovieController);
  187.             fMovieController = NULL;
  188.         }
  189.         
  190.         if (fMovie)
  191.         {
  192.             DisposeMovie(fMovie);
  193.             fMovie = NULL;
  194.         }
  195.             
  196.         fMovie = newMovie;
  197.         
  198.         ::SetMovieGWorld(fMovie,(CGrafPtr)this->GetXMPWindow()->GetPlatformWindow(), NULL);
  199.         
  200.         FW_SPlatformRect movieBox;
  201.         ::GetMovieBox(fMovie, &movieBox);
  202.         if (::EmptyRect(&movieBox))
  203.             ::SetRect(&movieBox, 0, 0, 100, 100);
  204.         fMovieController = ::NewMovieController(fMovie, &movieBox, mcTopLeftMovie+mcWithBadge+mcNotVisible);
  205.         THROW_IF_NULL(fMovieController);
  206.         ::MCDoAction(fMovieController, mcActionSetKeysEnabled, (Ptr)TRUE);
  207.         ::MCDoAction(fMovieController, mcActionSetUseBadge, (Ptr)TRUE);
  208.         ::MCEnableEditing(fMovieController, TRUE);
  209.     }
  210. }
  211.  
  212. //----------------------------------------------------------------------------------------
  213. // CMovieFacet::AdjustMovieBox
  214. //----------------------------------------------------------------------------------------
  215. void CMovieFacet::AdjustMovieBox()
  216. {
  217.     FW_CRect boundingBox;
  218.     fMovieFrame->GetFrameShapeBounds(&boundingBox);
  219.     
  220.     if (fMovie)
  221.     {
  222.         FW_SPlatformRect movieBox;
  223.         
  224.         if (fMovieFrame->IsRoot())
  225.             boundingBox.bottom -= ff(16);
  226.         boundingBox.Place(ff(0), ff(0));
  227.         boundingBox.AsPlatformRect(movieBox);
  228.         
  229.         ::SetMovieBox(fMovie, &movieBox);
  230.         THROW_IF_ERROR(::MCMovieChanged(fMovieController, fMovie), "::MCMovieChanged");
  231.     }
  232.     else
  233.         this->GetMoviePart()->GetDefaultPicture()->SetRectangle(boundingBox);
  234. }
  235.  
  236. //----------------------------------------------------------------------------------------
  237. // CMovieFacet::ClipShapeChanged
  238. //----------------------------------------------------------------------------------------
  239. void CMovieFacet::ClipShapeChanged()
  240. {
  241.     if (fMovieController)
  242.     {
  243.         FW_PlatformRegion movieClipRegion;
  244.         FW_PlatformRegion clipRegion;
  245.         
  246.         clipRegion = ::NewRgn();
  247.         ::CopyRgn(this->GetXMPFacet()->GetClipShape()->GetQDRegion(), clipRegion);
  248.         
  249.         movieClipRegion = ::GetMovieDisplayBoundsRgn(fMovie);
  250.         ::SectRgn(clipRegion, movieClipRegion, movieClipRegion);
  251.         
  252.         THROW_IF_ERROR(::MCSetClip(fMovieController, clipRegion, movieClipRegion), "::MCSetClip");
  253.         
  254.         ::DisposeRgn(clipRegion);
  255.         ::DisposeRgn(movieClipRegion);
  256.     }
  257. }
  258.  
  259. //----------------------------------------------------------------------------------------
  260. // CMovieFacet::StartMovie
  261. //----------------------------------------------------------------------------------------
  262. void CMovieFacet::StartMovie()
  263. {
  264.     if (fMovieController)
  265.     {
  266.         Fixed playRate = ::GetMoviePreferredRate(fMovie);
  267.         ::MCDoAction(fMovieController, mcActionPlay, (Ptr)playRate);
  268.     }
  269. }
  270.  
  271. //----------------------------------------------------------------------------------------
  272. // CMovieFacet::StopMovie
  273. //----------------------------------------------------------------------------------------
  274. void CMovieFacet::StopMovie()
  275. {
  276.     if (fMovieController)
  277.         ::MCDoAction(fMovieController, mcActionPlay, (Ptr)0);
  278. }
  279.  
  280. //----------------------------------------------------------------------------------------
  281. // CMovieFacet::SetMovieLooping
  282. //----------------------------------------------------------------------------------------
  283. void CMovieFacet::SetMovieLooping(FW_Boolean loop)
  284. {
  285.     if (fMovieController)
  286.         ::MCDoAction(fMovieController, mcActionSetLooping, (Ptr)loop);
  287. }
  288.  
  289. //----------------------------------------------------------------------------------------
  290. // CMovieFacet::Draw
  291. //----------------------------------------------------------------------------------------
  292. void CMovieFacet::Draw(FW_CGraphicContext *gc)
  293. {
  294.     if (fMovieController != NULL)
  295.         ::MCDraw(fMovieController, this->GetXMPFacet()->GetWindow()->GetPlatformWindow());
  296.     else
  297.         this->GetMoviePart()->GetDefaultPicture()->Draw(gc);
  298. }
  299.  
  300. //----------------------------------------------------------------------------------------
  301. // CMovieFacet::GeometryChanged
  302. //----------------------------------------------------------------------------------------
  303. void CMovieFacet::GeometryChanged()
  304. {
  305.     FW_CFacet::GeometryChanged();
  306.     
  307.     FW_CRect boundingBox;
  308.     fMovieFrame->GetFrameShapeBounds(&boundingBox);
  309.     
  310.     FW_CRect movieBox;
  311.     this->GetMovieBoundingBox(&movieBox);
  312.     
  313.     if (boundingBox != movieBox)
  314.         this->AdjustMovieBox();
  315.         
  316.     this->ClipShapeChanged();
  317. }
  318.  
  319. //------------------------------------------------------------------------------
  320. // CMovieFacet::WindowActivated
  321. //------------------------------------------------------------------------------
  322.  
  323. void CMovieFacet::WindowActivated()
  324. {
  325.     this->DoMovieActivate(TRUE);
  326. }
  327.  
  328. //------------------------------------------------------------------------------
  329. // CMovieFacet::WindowDeActivated
  330. //------------------------------------------------------------------------------
  331.  
  332. void CMovieFacet::WindowDeActivated()
  333. {
  334.     this->DoMovieActivate(FALSE);
  335. }
  336.  
  337. //----------------------------------------------------------------------------------------
  338. // CMovieFacet::DoMouseDown
  339. //----------------------------------------------------------------------------------------
  340. FW_Boolean CMovieFacet::DoMouseDown(const FW_CPoint& where, XMPEventData event)
  341. {
  342.     FW_Boolean eventHandled = FALSE;
  343.     
  344.     if (fMovieController)
  345.     {
  346.         FW_CGraphicContext gc(this->GetXMPFacet());
  347.         
  348.         FW_SPlatformPoint originalWhere;
  349.         ::SetPt(&originalWhere, event->where.h, event->where.v);
  350.         
  351.         FW_CPoint offset(0,0);
  352.         offset = this->GetContentTransform()->TransformPoint(offset);
  353.         ::SetOrigin(-(offset.IntX()), -(offset.IntY()));
  354.         
  355.         event->where.h = where.IntX(); event->where.v = where.IntY();
  356.         eventHandled = (FW_Boolean)::MCClick(fMovieController, this->GetXMPFacet()->GetWindow()->GetPlatformWindow(),
  357.                                         event->where, event->when, event->modifiers);
  358.         
  359.         ::SetPt(&event->where, originalWhere.h, originalWhere.v);
  360.                                         
  361.         this->GetFrame()->GetPart()->GetSelection()->Drag(this, event);
  362.     }
  363.     
  364.     return eventHandled;
  365. }
  366.  
  367. //----------------------------------------------------------------------------------------
  368. // CMovieFacet::DoMovieKey
  369. //----------------------------------------------------------------------------------------
  370. FW_Boolean CMovieFacet::DoMovieKey(XMPEventData event)
  371. {
  372.     FW_Boolean eventHandled = FALSE;
  373.     
  374.     if (fMovieController)
  375.     {
  376.         FW_CGraphicContext gc(this->GetXMPFacet());
  377.         
  378.         eventHandled = (FW_Boolean)::MCKey(fMovieController, (char)event->message, event->modifiers);
  379.     }
  380.     
  381.     return eventHandled;
  382. }
  383.  
  384. //----------------------------------------------------------------------------------------
  385. // CMovieFacet::DoMovieSetVisible
  386. //----------------------------------------------------------------------------------------
  387. void CMovieFacet::DoMovieSetVisible(FW_Boolean visible)
  388. {
  389.     if (fMovieController)
  390.     {
  391.         FW_CGraphicContext gc(this->GetXMPFacet());
  392.         
  393.         THROW_IF_ERROR(::MCSetVisible(fMovieController, visible), "::MCSetVisible");
  394.     }
  395. }
  396.  
  397. //----------------------------------------------------------------------------------------
  398. // CMovieFacet::DoMovieIdle
  399. //----------------------------------------------------------------------------------------
  400. FW_Boolean CMovieFacet::DoMovieIdle()
  401. {
  402.     FW_Boolean eventHandled = FALSE;
  403.     
  404.     if (fMovieController)
  405.     {
  406.         FW_CGraphicContext gc(this->GetXMPFacet());
  407.         
  408.         eventHandled = (FW_Boolean)::MCIdle(fMovieController);
  409.     }
  410.     
  411.     return eventHandled;
  412. }
  413.  
  414. //----------------------------------------------------------------------------------------
  415. // CMovieFacet::DoMovieActivate
  416. //----------------------------------------------------------------------------------------
  417. void CMovieFacet::DoMovieActivate(FW_Boolean activate)
  418. {
  419.     if (fMovieController)
  420.     {
  421.         FW_CGraphicContext gc(this->GetXMPFacet());
  422.         
  423.         ::MCActivate(fMovieController, this->GetXMPWindow()->GetPlatformWindow(), activate);
  424.     }
  425. }
  426.  
  427. //----------------------------------------------------------------------------------------
  428. // CMovieFacet::IsControllerVisible
  429. //----------------------------------------------------------------------------------------
  430. FW_Boolean CMovieFacet::IsControllerVisible()
  431. {
  432.     if (fMovieController)
  433.         return ::MCGetVisible(fMovieController);
  434.     else
  435.         return FALSE;
  436. }
  437.  
  438. //------------------------------------------------------------------------------
  439. // CMovieFacet::GetCurrentSelection
  440. //------------------------------------------------------------------------------
  441. void CMovieFacet::GetCurrentSelection(TimeValue* currentTime, TimeValue* duration)
  442. {
  443.     if (fMovie)
  444.         ::GetMovieSelection(fMovie, currentTime, duration);
  445. }
  446.  
  447. //------------------------------------------------------------------------------
  448. // CMovieFacet::SetCurrentSelection
  449. //------------------------------------------------------------------------------
  450. void CMovieFacet::SetCurrentSelection(TimeValue currentTime, TimeValue duration)
  451. {
  452.     if (fMovie)
  453.     {
  454.         FW_CGraphicContext gc(this->GetXMPFacet());
  455.         
  456.         ::SetMovieSelection(fMovie, currentTime, duration);
  457.         THROW_IF_ERROR(::MCMovieChanged(fMovieController, fMovie), "::MCMovieChanged");
  458.     }
  459. }
  460.  
  461. //------------------------------------------------------------------------------
  462. // CMovieFacet::IsEmpty
  463. //------------------------------------------------------------------------------
  464. FW_Boolean CMovieFacet::IsEmpty()
  465. {
  466.     FW_Boolean empty = TRUE;
  467.     if (fMovie)
  468.     {
  469.         TimeValue currentTime, duration;
  470.         
  471.         ::GetMovieSelection(fMovie, ¤tTime, &duration);
  472.         empty = duration == (TimeValue)0;
  473.     }
  474.     return empty;
  475. }
  476.  
  477. //----------------------------------------------------------------------------------------
  478. // CMovieFacet::CloseSelection
  479. //----------------------------------------------------------------------------------------
  480. void CMovieFacet::CloseSelection()
  481. {
  482.     this->SetCurrentSelection(0, 0);
  483. }
  484.  
  485. //------------------------------------------------------------------------------
  486. // CMovieFacet::ClearSelection
  487. //------------------------------------------------------------------------------
  488. void CMovieFacet::ClearSelection(TimeValue currentTime, TimeValue duration)
  489. {
  490.     if (fMovieController)
  491.     {
  492.         FW_CGraphicContext gc(this->GetXMPFacet());
  493.         
  494.         this->SetCurrentSelection(currentTime, duration);
  495.         THROW_IF_ERROR(::MCClear(fMovieController));
  496.     }
  497. }
  498.  
  499. //------------------------------------------------------------------------------
  500. // CMovieFacet::SelectAll
  501. //------------------------------------------------------------------------------
  502. void CMovieFacet::SelectAll()
  503. {
  504.     if (fMovie)
  505.     {
  506.         FW_CGraphicContext gc(this->GetXMPFacet());
  507.         
  508.         this->SetCurrentSelection((TimeValue)0, ::GetMovieDuration(fMovie));
  509.     }
  510. }
  511.  
  512. //------------------------------------------------------------------------------
  513. // CMovieFacet::CopySelection
  514. //------------------------------------------------------------------------------
  515. FW_PlatformHandle CMovieFacet::CopySelection()
  516. {
  517.     FW_PlatformHandle movieHandle = NULL;
  518.     if (fMovieController)
  519.     {
  520.         FW_CGraphicContext gc(this->GetXMPFacet());
  521.         
  522.         Movie newMovie = ::MCCopy(fMovieController);
  523.         movieHandle = ::FW_CMemoryManager::AllocateSystemHandle(0);
  524.         THROW_IF_NULL(movieHandle);
  525.         THROW_IF_ERROR(::PutMovieIntoHandle(newMovie, movieHandle));
  526.         DisposeMovie(newMovie);
  527.     }
  528.     return movieHandle;
  529. }
  530.  
  531. //------------------------------------------------------------------------------
  532. // CMovieFacet::DragEnter
  533. //------------------------------------------------------------------------------
  534. void CMovieFacet::DragEnter(XMPDragItemIterator* dragInfo, const FW_CPoint& where)
  535. {
  536.     FW_CFacet::DragEnter(dragInfo, where);
  537.     
  538.     if (!this->GetCanAcceptDrop())
  539.     {
  540.         XMPStorageUnit *dragSU;
  541.         HFSFlavor *valueData;
  542.         long valueSize;
  543.         
  544.         XMPTranslation *translate = GetFrame()->GetPart()->GetSession()->GetTranslation();
  545.         XMPType typeHFS = translate->GetISOTypeFromPlatformType('hfs ', kXMPPlatformDataType);
  546.         XMPType typePICT = translate->GetISOTypeFromPlatformType('PICT', kXMPPlatformDataType);
  547.         XMPType typeTEXT = translate->GetISOTypeFromPlatformType('TEXT', kXMPPlatformDataType);
  548.         XMPType typesnd = translate->GetISOTypeFromPlatformType('snd ', kXMPPlatformDataType);
  549.  
  550.         for (dragSU = dragInfo->First(); dragSU; dragSU = dragInfo->Next())
  551.         {
  552.             if (fMovie && (dragSU->Exists(kXMPPropContents, typePICT, 0) ||
  553.                     dragSU->Exists(kXMPPropContents, typeTEXT, 0) ||
  554.                     dragSU->Exists(kXMPPropContents, typesnd, 0)))
  555.             {
  556.                 this->SetCanAcceptDrop(TRUE);
  557.             }
  558.             else if (dragSU->Exists(kXMPPropContents, typeHFS, 0))
  559.             {
  560.                 dragSU->Focus(kXMPPropContents, kXMPPosUndefined, typeHFS, 0, kXMPPosUndefined);        
  561.                 valueSize = dragSU->GetSize();
  562.                 if (valueSize)
  563.                 {
  564.                     valueData = (HFSFlavor*) FW_CMemoryManager::AllocateBlock(valueSize);
  565.                     dragSU->GetValue(valueSize, (XMPValue)valueData);
  566.                     if (valueData->fileType == 'MooV')
  567.                         this->SetCanAcceptDrop(TRUE);
  568.                     FW_CMemoryManager::FreeBlock(valueData);
  569.                 }
  570.             }
  571.         }
  572.     }
  573. }
  574.  
  575. //------------------------------------------------------------------------------
  576. // CMovieFacet::DrawDragHiliteInside
  577. //------------------------------------------------------------------------------
  578. FW_Boolean CMovieFacet::DrawDragHiliteInside()
  579. {
  580.     if (this->GetFrame()->IsRoot())
  581.         return TRUE;
  582.     else
  583.         return FALSE;
  584. }
  585.  
  586. //------------------------------------------------------------------------------
  587. // CMovieFacet::GetMovieAsPict
  588. //------------------------------------------------------------------------------
  589. PicHandle CMovieFacet::GetMovieAsPict()
  590. {
  591.     PicHandle moviePict = NULL;
  592.     if (fMovie)
  593.         moviePict = ::GetMoviePict(fMovie, ::GetMovieTime(fMovie, NULL));
  594.     return moviePict;
  595. }
  596.  
  597. //----------------------------------------------------------------------------------------
  598. // CMovieFacet::PasteMovieToSelection
  599. //----------------------------------------------------------------------------------------
  600. void CMovieFacet::PasteMovieToSelection(Movie newMovie, TimeValue currentTime, TimeValue duration)
  601. {
  602.     if (fMovieController)
  603.     {
  604.         FW_CGraphicContext gc(this->GetXMPFacet());
  605.         
  606.         this->SetCurrentSelection(currentTime, duration);
  607.         THROW_IF_ERROR(::MCPaste(fMovieController, newMovie));
  608.     }
  609. }
  610.  
  611. //----------------------------------------------------------------------------------------
  612. // CMovieFacet::PasteHandleToSelection
  613. //----------------------------------------------------------------------------------------
  614. void CMovieFacet::PasteHandleToSelection(FW_PlatformHandle newHandle, OSType handleType, TimeValue currentTime, TimeValue duration)
  615. {
  616.     if (fMovie)
  617.     {
  618.         FW_CGraphicContext gc(this->GetXMPFacet());
  619.         
  620.         this->SetCurrentSelection(currentTime, duration);
  621.         THROW_IF_ERROR(::PasteHandleIntoMovie(newHandle, handleType, fMovie, 0, (ComponentInstance)0));
  622.         THROW_IF_ERROR(::MCMovieChanged(fMovieController, fMovie), "::MCMovieChanged");
  623.     }
  624. }
  625.  
  626.